﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;

/// <summary>
/// Summary description for Paging
/// </summary>
public class Paging
{
	public Paging()
	{
		
	}

    public string DoPaging(int thisPageNo, int totalCount, int pageSize, string url)
    {
        if (totalCount.Equals(0))
        {
            return "";
        }

        int pageno = 0;
        int start = 0;
        int loop = totalCount / pageSize;
        int remainder = totalCount % pageSize;
        int startPageNoFrom = thisPageNo - 6;
        int endPageNoTo = thisPageNo + 6;
        int lastRenderedPageNo = 0;

        StringBuilder strB = new StringBuilder("<div  class=\"pagenunber\">", 500);

        // write 1st if required
        if (startPageNoFrom >= 1)
        {
            strB.Append("<a href=#"+pageno+" onclick=\"javascript:LoadGridViewData(0, 1,'" + url + "','Page')\" title=\"Result Pages 1\">1</a>  ");
            if (!startPageNoFrom.Equals(1))
            {
                strB.Append(" ...  ");
            }
        }

        for (int i = 0; i < loop; i++)
        {
            pageno = i + 1;
            if (pageno > startPageNoFrom && pageno < endPageNoTo)
            {
                if (pageno.Equals(thisPageNo))
                    strB.Append("<a class=\"selected\">" + pageno + "</a>&nbsp; ");
                else
                    strB.Append("<a href=#" + pageno + " onclick=\"javascript:LoadGridViewData(" + start + ", " + pageno + ",'" + url + "#" + pageno + "'  ,'Page')\" title=\"Result Pages " + pageno + "\">" + pageno + "</a> ");

                lastRenderedPageNo = pageno;
            }
            start += pageSize;
        }

        // write ... if required just before end
        if (!pageno.Equals(lastRenderedPageNo))
        {
            strB.Append(" ...  ");
        }

        if (remainder > 0)
        {
            pageno++;
            if (pageno.Equals(thisPageNo))
                strB.Append("<a href=#" +pageno+ " class=\"selected\">" + pageno + "</a>");
            else
                strB.Append("<a href=#" + pageno + " onclick=\"javascript:LoadGridViewData(" + start + ", " + pageno + ",'" + url + "#" + pageno + "','Page')\" title=\"Page " + pageno + "\">" + pageno + "</a>  ");
        }
        else // write last page number
        {
            if (loop >= endPageNoTo)
            {
                if (loop.Equals(thisPageNo))
                    strB.Append("<a  class=\"selected\">" + loop + "</a>&nbsp; ");
                else
                    strB.Append("<a href=#" + pageno + " onclick=\"javascript:LoadGridViewData(" + start + ", " + pageno + ",'" + url + "#" + pageno + "','Page')\" title=\"Page " + loop + "\">" + loop + "</a>  ");
            }
        }

        return strB.ToString() + "</div>";
    }


}

